# Standard library imports
from sgmllib import SGMLParser
import csv

HEADINGS = ['date',
            'year',
            'company_name',
            'country',
            'area',
            'sic',
            'sector',
            'pct4sale',
            'transaction_value',
            'sale_method',
            'market_area']


class PrivatizationBarometerParser(SGMLParser):

    def __init__(self, csv_path):
        self._csv_path = csv_path
        SGMLParser.__init__(self)

    def reset(self):
        self._first_tr = True
        self._in_td = False
        self._td_contents = []
        self._line = HEADINGS
        self._f = file(self._csv_path, 'wb')
        self._writer = csv.writer(self._f)
        SGMLParser.reset(self)

    def start_tr(self, attributes):
        pass

    def end_tr(self):
        if self._first_tr:
            self._first_tr = False
        self.flushLine()

    def start_td(self, attributes):
        self._in_td = True

    def end_td(self):
        self._in_td = False
        self._line.append(' '.join(self._td_contents))
        self._td_contents = []

    def handle_data(self, data):
        if self._in_td and not self._first_tr:
            self._td_contents.append(data)

    def handle_entityref(self, ref):
        pass

    def flushLine(self):
        self._writer.writerow(self._line)
        self._line = []


def setup(folderpath, urlopener, force=False):
    p = PrivatizationBarometerParser('%s/all_results.csv' % folderpath)
    f = file('%s/all_results.html' % folderpath, 'r')
    p.feed(f.read())
    f.close()
    p._f.close()
